#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
商户渠道信息查询模型
"""

from typing import Dict, Any, Optional, List
from dataclasses import dataclass, asdict
from jlpay.model.base import OrgBaseReq, BaseResponse


class MerchChannelInfoQueryRequest(OrgBaseReq):
    """商户渠道信息查询请求参数"""
    
    API_URL = "/open/merch/access/merch-channel-info/query"
    
    def __init__(self, merch_no: str = ""):
        """初始化商户渠道信息查询请求"""
        super().__init__()
        self.merch_no = merch_no
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return {k: v for k, v in self.__dict__.items() if not k.startswith('_') and v is not None}


@dataclass
class ChannelInfo:
    """渠道信息"""
    channel_id: str = ""  # 渠道ID
    report_status: str = ""  # 报备状态 (对应Java的reportedFlag)
    channel_merch_no: str = ""  # 渠道商户号
    fail_reason: str = ""  # 失败原因
    channel_merch_name: str = ""  # 渠道商户名称
    channel_merch_shortname: str = ""  # 渠道商户经营名称 (对应Java的channelBusinessName)
    report_time: str = ""  # 报备时间 (对应Java的reportedTime)
    channel_org_id: str = ""  # 渠道服务商编号
    fee_type: str = ""  # 费率类型 (对应Java的rateType)

    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return asdict(self)


class MerchChannelInfoQueryResponse(BaseResponse):
    """商户渠道信息查询响应参数"""
    
    def __init__(self, response_dict=None):
        """
        初始化响应对象
        
        Args:
            response_dict: 响应字典
        """
        # 先调用父类初始化，处理ret_code, ret_msg等基础字段
        super().__init__(response_dict)
        
        # 初始化特定字段
        self.merch_no = ""  # 商户号
        self.merch_name = ""  # 商户名称
        self.channel_info = []  # 渠道信息
        
        if response_dict:
            # 从response_dict中提取特定字段，避免与父类字段冲突
            self.merch_no = response_dict.get('merch_no', '')
            self.merch_name = response_dict.get('merch_name', '')
            self.channel_info = []
            
            # 处理渠道信息列表
            channel_info_list = response_dict.get('channel_info', [])
            if channel_info_list:
                for channel_data in channel_info_list:
                    if isinstance(channel_data, dict):
                        self.channel_info.append(ChannelInfo(**channel_data))
                    else:
                        self.channel_info.append(channel_data)

    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data['merch_no'] = self.merch_no
        data['merch_name'] = self.merch_name
        data['channel_info'] = [channel.to_dict() for channel in self.channel_info]
        return data 